﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NIP.Speech.Dialog.States
{
    public class SessionAwaitingCommandDialogState: DialogStateBase,IDialogState 
    {

        public SessionAwaitingCommandDialogState()
            : base(DialogStateType.SessionAwaitingCommandDialogState)
        {
            
        }
        new public bool PreConditionCheck()
        {
            return true;
        }

        new public EventArgs.StateStatusEventArgs VoiceCommandResponse(Dictionary<string, string> recognizedText)
        {
            var args = new EventArgs.StateStatusEventArgs();
            string queryResponse = "";
            if (recognizedText.TryGetValue("if_yes", out queryResponse))
            {
                recognizedText.Remove(queryResponse);
                recognizedText.Add(queryResponse," ");
            }
            if (recognizedText.TryGetValue("if_no", out queryResponse))
            {
                recognizedText.Remove(queryResponse);
                recognizedText.Add(queryResponse, " ");
                args.ToSpeak += "Would you like me to sketch a portrait of yourself?";
                args.nextState = Responder.StateCreator(DialogStateType.QueryDialogState);
                args.nextState.para.configCommand.Add("if_yes", "draw");
                args.nextState.para.configCommand.Add("if_no", "none");
                args.nextState.DefaultAbortNextState = DialogStateType.SessionAwaitingCommandDialogState;
                args.nextState.DefaultNextState = DialogStateType.SessionAwaitingCommandDialogState;
                args.nextState.DefaultSpeechResponse += " . Yes or No ? ";
                return args;
            }

            if (recognizedText.ContainsKey("draw"))
            {
                args.Type = EventArgs.StateStatusEventTypes.ACTION_REQUESTED;
                args.ActionRequested = "draw";
                args.ObjectName = "human";
            }
            else if(recognizedText.ContainsKey("command"))
            {
                if (recognizedText.ContainsKey("action"))
                {
                    args.ActionRequested = recognizedText["action"];
                    args.Type = EventArgs.StateStatusEventTypes.ACTION_REQUESTED;
                }
                else if (recognizedText.ContainsKey("commandobject"))
                {
                    args.ObjectName = recognizedText["commandobject"];
                    args.ActionRequested = "get";
                    args.Type = EventArgs.StateStatusEventTypes.ACTION_REQUESTED;
                }
                
            } else if (recognizedText.ContainsKey("help")){
                args.Type = EventArgs.StateStatusEventTypes.SPEAK_ONLY;
                args.ToSpeak = "I can sketch you a portrait that you can take home ";//I can sketch you a portrait that you can take home
                return args;
            }
           
            // if neither was received, unchanged, else move on
            if (args.Type == EventArgs.StateStatusEventTypes.ACTION_REQUESTED)
            {

                args.ToSpeak += " Performing the following action ";
                args.ToSpeak += args.ActionRequested + " the " + args.ObjectName;
                args.ToSpeak += " please say Abort to skip the command";
                args.nextState = Responder.StateCreator(DialogStateType.Action_In_Progress);
                return args;
            }
            else
            {
                args.Type = EventArgs.StateStatusEventTypes.SPEAK_ONLY;
                args.ToSpeak = this.DefaultSpeechResponse;
            }
            return args;
        }

        new public EventArgs.StateStatusEventArgs VoiceCommandResponse(string recognizedString)
        {
            throw new NotImplementedException();
            //! Initiate Session
            //var args = new EventArgs.StateStatusEventArgs();  
            //args.CommandString = recognizedString;

            //if (recognizedString.ToLower().IndexOf("help") >= 0)
            //{
            //    args.Type = EventArgs.StateStatusEventTypes.SPEAK_ONLY;
            //    args.ToSpeak = "I can understand objects name, find and bring them back to you. I only know a few objects as followed: block, can.";
            //    args.NextState = DialogStateType.SessionAwaitingCommandDialogState;
               
            //}

            //if (recognizedString.ToLower().IndexOf("home") > 0)
            //{
            //    args.Type = EventArgs.StateStatusEventTypes.ACTION_REQUESTED;
            //    args.NextState = DialogStateType.SessionAwaitingCommandDialogState;
            //}
            //return args;

            
        }
    }

}

